package com.example.restaurant.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.example.restaurant.common.Log;
import com.example.restaurant.common.Result;
import com.example.restaurant.controller.form.StoreForm;
import com.example.restaurant.controller.form.UpdateStoreForm;
import com.example.restaurant.model.Store;
import com.example.restaurant.service.StoreService;
import com.example.restaurant.service.param.StoreParam;
import com.example.restaurant.service.param.UpdateStoreParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 门店表 前端控制器
 * </p>
 *
 * @author x
 * @since 2022-09-29
 */
@RestController
//@RequestMapping("/restaurant/store")
@Slf4j
public class StoreController {
    @Resource
    private StoreService storeService;

    //新增门店
    @PostMapping("addStore")
    @Log
    public Result addStore(@RequestBody @Valid StoreForm storeForm){
        log.info("{}",storeForm.toString());
        StoreParam storeParam = BeanUtil.copyProperties(storeForm, StoreParam.class);
        Object userId = StpUtil.getExtra("userId");
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
        storeParam.setUserId(i);
        boolean b = storeService.addStore(storeParam);
        return Result.success();
    }

    //查询旗下所有门店 根据商户id
    @GetMapping("getStore")
    @Log
    public Result getStore(){
        Object userId = StpUtil.getExtra("userId");
        log.info("{}",userId);
        String userId1 = String.valueOf(userId);
        int i = Integer.parseInt(userId1);
        List<Store> store = storeService.getStore(i);
        return Result.success(store);
    }
    //修改门店信息 根据门店id
    @PutMapping("modifyStore")
    @Log
    public Result modifyStore(@RequestBody @Valid UpdateStoreForm updateStoreForm){
        UpdateStoreParam updateStoreParam = BeanUtil.copyProperties(updateStoreForm, UpdateStoreParam.class);
        boolean b = storeService.modifyStore(updateStoreParam);
        return Result.success();
    }

    //删除门店
    @DeleteMapping("delStore")
    @Log
    public Result delStore(int storeId){
        log.info("{}",storeId);
        boolean b = storeService.delStore(storeId);
        return Result.success();
    }

    //获取所有门店
    @GetMapping("/getAllStore")
    public Result getAllStore(){
        List<Store> allStore = storeService.getAllStore();
        return Result.success(allStore);
    }


}
